<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Class FoodType
 * @package App\Models
 */
class FoodType extends Model
{
    protected $fillable = [
        'name',
        'description',
        'sort',
        'status',
        'school_id'
    ];

    /**
     * @param $query
     * @param $name
     */
    public function scopeNameLike($query, $name)
    {
        $query->when($name, function ($query) use ($name) {
            $query->where('name', 'like', $name . '%');
        });
    }

    /**
     * @param $query
     * @param $schoolId
     */
    public function scopeSchoolIs($query, $schoolId)
    {
        $query->when($schoolId, function ($query) use ($schoolId) {
            $query->where('school_id', $schoolId);
        });
    }

    /**
     * @param $query
     * @param $status
     */
    public function scopeStatusIs($query, $status)
    {
        $query->when($status, function ($query) use ($status) {
            $query->where('status', $status);
        });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function school()
    {
        return $this->belongsTo(School::class);
    }

    /**
     * @return mixed
     */
    public function getCountAttribute()
    {
        return Food::where('food_type_id', $this->id)->where('status', Food::START_USING)->count();
    }
}